<?php
//include_once("../modules/checkA.php");
//include_once("../../config.php");
//?>



<?
require_once ROOT_PATH_.'/application/PHPExcel/PHPExcel.php';
require_once ROOT_PATH_.'/application/PHPExcel/PHPExcel/Writer/Excel2007.php';
require_once ROOT_PATH_.'/application/PHPExcel/PHPExcel/Writer/Excel5.php';
require_once ROOT_PATH_.'/application/PHPExcel/PHPExcel/IOFactory.php';
?>



<?


date_default_timezone_set('Europe/London'); //设置时间



$inputFileName = ROOT_PATH_.'/uploadfiles/'.$name;

//判断一下，文件是2007还是旧版的excel
if(strstr($name,".")==".xls")
{
    $inputFileType = 'Excel5';
}
else if(strstr($name,".")==".xlsx")
{
    $inputFileType = 'Excel2007';
}
else
{
    echo "请检查文件格式";
}



//echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' using IOFactory with a defined reader type of ',$inputFileType,'<br />';
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
/*
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow(); //取得总行数
$highestColumn = $sheet->getHighestColumn(); //取得总列
*/
$objWorksheet = $objPHPExcel->getActiveSheet();//取得总行数
$highestRow = $objWorksheet->getHighestRow();//取得总列数

echo 'highestRow='.$highestRow;
//echo "<br>";
$highestColumn = $objWorksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);//总列数
echo 'highestColumnIndex='.$highestColumnIndex;
//echo "<br />";



$headtitle=array();
$result=array();




for ($row = 0;$row <= $highestRow;$row++)
{
    $strs=array();
    //注意highestColumnIndex的列数索引从0开始
    for ($col = 0;$col < $highestColumnIndex;$col++)
    {
        $strs[$col] =$objWorksheet->getCellByColumnAndRow($col, $row)->getValue();

         $result[$row][$col]=$strs[$col];
    }


}







//设置需要搜索的表格的前缀后缀
$table_Suffix = "refueling";
$table = $table_Prefix . $table_Suffix;
//设置需要搜索的表格的前缀后缀





$date=date("Y-m-d h:m:s");



$equipment_fulllist  = select_lines_all_items($table_Prefix . "equipment", "nid desc", "", "0");
$equipment_list      = transform_2darray_in_1darray_with_new_key($equipment_fulllist, "name", "nid");


//用foreach去掉名字项目所有字母和中文，因为客户只输入数字不输入全部完整的车牌

foreach ($equipment_list as $i => $v) {


preg_match_all('/\d+/',$i,$arr);
$arr = join('',$arr[0]);

        $equipment_list_lite[$arr]=$v;
}


//print_r2($equipment_list_lite);



$employee_fulllist  = select_lines_all_items($table_Prefix . "employee", "nid desc", "", "0");
$employee_list      = transform_2darray_in_1darray_with_new_key($employee_fulllist, "name", "nid");



for($i=0;$i<count($result);$i++) {


    //0日期，1车号，2序号（没用），3单价，4姓名，5数量，6金额，7里程数,8油品

    //导入的人的名称
    $authur = $_SESSION['username'];


    //安全起见，把excel读取的车牌，也提纯一下数字

        preg_match_all('/\d+/',$result[$i][1],$arr);
        $arr = join('',$arr[0]);
        $result[$i][1]=$arr;


    if (is_numeric($result[$i][2])) {

        $values = "('".$result[$i][8]."','" . $equipment_list_lite[$result[$i][1]] . "','" . $result[$i][5] . "','" . $employee_list[$result[$i][4]] . "','" . $result[$i][6] . "','" . $result[$i][0] . "','" . $result[$i][7] . "','" . $result[$i][3] . "','" . $authur . "')," . $values;


    }

    else
    {
        echo $i.":".$result[$i][1]."不是整形";
        echo "<br/>";
    }

}




echo "<br/>";
echo $values=rtrim($values, ',');
echo "<br/>";



echo $sql = "INSERT INTO $table (
type,
equipmentid,
number,
principal,
cost,
applydate,
mileage,
price,
authur
)

value $values";



$query = mysql_query($sql) or die ("error:".mysql_error());
mysql_close($con);








?>